//
//  YTStudyViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/1.
//

#import "YTStudyViewController.h"
#import "YTStashTJModel.h"
#import "YTStudyTableViewCell.h"
#import "YTStudyHeaderView.h"
#import "YTStudyOneViewController.h"
#import "YTStudyDetailsViewController.h"
#import "YTVideoViewController.h"

@interface YTStudyViewController ()

@property (nonatomic ,strong) UIButton * XXBtn;
@property (nonatomic ,strong) UIButton * ZJBtn;
@property (nonatomic ,strong) UIView * Btnmakeview;
///当前是否是专家
@property (nonatomic ,assign) BOOL isOpenXX;
@property (nonatomic ,strong) NSMutableArray <YTStashTJModel *>* TJModelArray;
@property (nonatomic ,strong) NSMutableArray <YTStashOneModel *>* BTModelArray;

@end

@implementation YTStudyViewController

- (NSMutableArray<YTStashTJModel *> *)TJModelArray{
    if(!_TJModelArray){
        _TJModelArray = [NSMutableArray array];
    }
    return _TJModelArray;
}

- (NSMutableArray<YTStashOneModel *> *)BTModelArray{
    if(!_BTModelArray){
        _BTModelArray = [NSMutableArray array];
    }
    return _BTModelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.NoDataHidden = YES;
    [self setupView];
    
}
- (void)setupView{
    self.view.backgroundColor = KcolorHex(@"#ECEDEE");
    MJWeakSelf
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.offset(0);
    }];
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
    [self addFooterView:^{
        [weakSelf refreshingData];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self openBtApi];
}
- (void)openBtApi{
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:@(1) forKey:@"level"];
    [SCNetManger learnTypeTypeOptionWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:YTStashOneModel.class json:model.result];
            self.BTModelArray = [NSMutableArray arrayWithArray:array];
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (void)loadNewData{
    ///走2个接口一个1级列表和推荐
    [self.view showBusyHUD];
    
    [SCNetManger learnInfoRecommendedWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:YTStashTJModel.class json:model.result];
            [self.TJModelArray removeAllObjects];
            [self.tableview.mj_header endRefreshing];
            if(array.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            [self.TJModelArray addObjectsFromArray:array];
            
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
            
        }
        
    }];
    
    
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YTStudyHeaderView * view = [YTStudyHeaderView initview];
    view.BTModelArray = self.BTModelArray;
    MJWeakSelf
    view.openBlock = ^(YTStashOneModel * _Nonnull model) {
        YTStudyOneViewController * vc = [YTStudyOneViewController new];
        vc.parentId = model.ID;
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  ceil(self.BTModelArray.count / 5.0) * kwt(75) + kwt(8 + 46);
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(self.TJModelArray.count < 1){
        UIView * footview = [UIView new];
        footview.clipsToBounds = YES;
        UIView * backview = [UIView new];
        [DTKTool YJ:kwt(10) andView:backview];
        backview.backgroundColor = KcolorHex(@"#F2F7FF");
        [footview addSubview:backview];
        [backview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(10));
            make.right.bottom.offset(kwt(-10));
            make.top.offset(kwt(10));
        }];
        
        UIImageView * noImage = [UIImageView new];
        [backview addSubview:noImage];
        noImage.image = [UIImage imageNamed:@"no_data"];
        [noImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_offset(0);
            make.centerY.mas_offset(kwt(-50));
        }];
        
        UILabel * noTitleLab = [UILabel new];
        [backview addSubview:noTitleLab];
        noTitleLab.text = @"暂无数据";
        noTitleLab.font = kFont(16);
        noTitleLab.textColor = [UIColor colorWithHexString:@"#686868"];
        [noTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(noImage.mas_bottom).offset(30);
            make.centerX.mas_offset(0);
        }];
        
        return footview;
    }
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if(self.TJModelArray.count < 1){
        return KSCREEN_HEIGHT - (KTop + kwt(56) + KSafeAreaTopHeight) - (ceil(self.BTModelArray.count / 5.0) * kwt(75) + kwt( 8 + 46));
    }
    return 0;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.TJModelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.TJModelArray[indexPath.row].CellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YTStudyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTStudyTableViewCell"];
    if (cell == nil) {
        cell = [[YTStudyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTStudyTableViewCell"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.TJModelArray[indexPath.row];
    MJWeakSelf
    cell.ReloadDataBlock = ^{
        [weakSelf.tableview reloadData];
    };
    cell.openvVideoBlock = ^(NSString * _Nonnull url) {
        YTVideoViewController * vc = [YTVideoViewController new];
        vc.url = url;
        vc.titleStr = weakSelf.TJModelArray[indexPath.row].title;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.TJModelArray[indexPath.row].displayForm isEqualToString:@"1"]){
//        [DTKTool openImagAndVideo:@[self.TJModelArray[indexPath.row].learnFileUrl] andopenview:self.view andIndex:0];
        YTVideoViewController * vc = [YTVideoViewController new];
        vc.url = self.TJModelArray[indexPath.row].learnFileUrl;
        vc.titleStr = self.TJModelArray[indexPath.row].title;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else{
        YTStudyDetailsViewController * vc = [YTStudyDetailsViewController new];
        vc.ID = self.TJModelArray[indexPath.row].ID;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


@end
